layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
   window.layedit = layui.layedit;

    //获取路径参数
    var request = GetRequest();

    $.get("/produce/" + request.id,function (data) {
        $("#id").val(data.id);
        $("#name").val(data.name);
        $("#price").val(data.price);
        $("#remarks").val(data.remarks);
        $("#originInformation").val(data.originInformation);

        //产品图片
        $("#pic").val(data.pic);
        if (data.pic != null && data.pic != "") {
            var picArr = data.pic.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //采用标准
        if (data.adoptStandard != null && data.adoptStandard != "") {
            var picArr = data.adoptStandard.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg1').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //质检报告
        if (data.qualityInspectionReport != null && data.qualityInspectionReport != "") {
            var picArr = data.qualityInspectionReport.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg2').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //质量管理体系认证书
        if (data.certificateOfQuality != null && data.certificateOfQuality != "") {
            var picArr = data.certificateOfQuality.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg3').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //产品许可证
        if (data.productLicense != null && data.productLicense != "") {
            var picArr = data.productLicense.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg4').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //产品类型
        treeSelect.render({
            elem: '#tree-select-type',
            data: '/produce/category/tree/select',
            type: 'get',
            placeholder: '顶级分类',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function(data){
                $("#tree-select-type").val(data.current.id);
            },
            success: function (item) { // 加载完成后的回调函数
                if (data.categoryId != null && data.categoryId != "") {
                    $("#tree-select-type").val(data.categoryId);
                } else {
                    $("#tree-select-type").val("0");
                }
                treeSelect.checkNode('tree-select-type',data.categoryId);
            }
        });

        //企业列表
        $.get("/company/list",function (company) {
            $.each(company.data,function (i, ele) {
                if (ele.id == data.manufactorId) {
                    $("#manufactorId").append('<option selected="selected" value="'+ ele.id +'">'+ ele.companeName +'</option>');
                } else {
                    $("#manufactorId").append('<option value="'+ ele.id +'">'+ ele.companeName +'</option>');
                }
            });
            form.render('select');
        });
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

